package com.tarena.lbs.message.web.service;

import com.alibaba.nacos.common.utils.CollectionUtils;
import com.tarena.lbs.common.passport.principle.UserPrinciple;
import com.tarena.lbs.common.security.utils.LbsSecurityContenxt;
import com.tarena.lbs.message.web.repository.MessageRepository;
import com.tarena.lbs.pojo.message.po.MessagePO;
import com.tarena.lbs.pojo.message.query.MessageQuery;
import com.tarena.lbs.pojo.message.vo.MessageVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class MessageService {
    @Autowired
    private MessageRepository messageRepository;
    public List<MessageVO> selectAll() {
        List<MessagePO> pos=messageRepository.selectAll();
        List<MessageVO> vos=null;
        if (CollectionUtils.isNotEmpty(pos)){
            vos=pos.stream().map(po->{
                MessageVO vo=new MessageVO();
                BeanUtils.copyProperties(po,vo);
                return vo;
            }).collect(Collectors.toList());
        }
        return vos;
    }

    public List<MessageVO> selectUserNewMessage(MessageQuery messageQuery) {
        UserPrinciple loginUser = LbsSecurityContenxt.getLoginToken();
        if (loginUser != null) {
            messageQuery.setUserId(loginUser.getId());
        }
        List<MessagePO> pos=messageRepository.getUserMessages(messageQuery);
        List<MessageVO> vos=null;
        if (CollectionUtils.isNotEmpty(pos)){
            vos=pos.stream().map(po->{
                MessageVO vo=new MessageVO();
                BeanUtils.copyProperties(po,vo);
                return vo;
            }).collect(Collectors.toList());
        }
        return vos;
    }
}
